<template>
	<a-row :gutter="10">
		<a-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
			<a-card :bordered="false" :loading="cardLoading">
				<a-tree
					v-if="treeData.length > 0"
					v-model:expandedKeys="defaultExpandedKeys"
					:tree-data="treeData"
					:field-names="treeFieldNames"
					@select="treeSelect"
				>
				</a-tree>
				<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE"/>
			</a-card>
		</a-col>
		<a-col :xs="24" :sm="24" :md="24" :lg="19" :xl="19">
			<a-card :bordered="false">
				<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState"
						class="ant-advanced-search-form">
					<a-row :gutter="24">
						<a-col :span="6">
							<a-form-item label="所属门店" name="parentId">
								<xn-page-select
									ref="xnparentIdPageSelectRef"
									v-model:value="searchFormState.parentId"
									placeholder="请选择所属门店"
									allow-clear
									show-search
									:page-function="parentIdSelector"
								/>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="仓库名称" name="storeName">
								<a-input v-model:value="searchFormState.storeName" placeholder="请输入仓库名称"/>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="仓库地址" name="storeAddress">
								<a-input v-model:value="searchFormState.storeAddress"
										 placeholder="请输入仓库地址"/>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
							<a-button style="margin: 0 8px" @click="reset">重置</a-button>
						</a-col>
					</a-row>
				</a-form>
				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:tool-config="toolConfig"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()"
									  v-if="hasPerm('supplierWarehouseStoragesAdd')">
								<template #icon>
									<plus-outlined/>
								</template>
								新增
							</a-button>
							<xn-batch-delete
								v-if="hasPerm('supplierWarehouseStoragesBatchDelete')"
								:selectedRowKeys="selectedRowKeys"
								@batchDelete="deleteBatchSupplierStorages"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'storeImage'">
							<div v-if="record.storeImage">
								<a-image :src="record.storeImage" style="width: 30px; height: 30px"/>
							</div>
							<span v-else>未上传</span>
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a @click="formRef.onOpen(record)"
								   v-if="hasPerm('supplierWarehouseStoragesEdit')">编辑</a>
								<a-divider type="vertical"
										   v-if="hasPerm(['supplierWarehouseStoragesEdit', 'supplierWarehouseStoragesDelete'], 'and')"/>
								<a-popconfirm title="确定要删除吗？" @confirm="deleteSupplierStorages(record)">
									<a-button type="link" danger size="small"
											  v-if="hasPerm('supplierWarehouseStoragesDelete')">
										删除
									</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
	</a-row>
	<Form ref="formRef" @successful="tableRef.refresh()"/>
</template>

<script setup name="supplierWarehouseStorages">
import {cloneDeep, isEmpty} from 'lodash-es'
import Form from './form.vue'
import {Empty} from "ant-design-vue";
import supplierStoragesApi from '@/api/supplier/warehouse/supplierStoragesApi'
import supplierStoresApi from "@/api/supplier/base/supplierStoresApi";

const searchFormState = ref({})
const searchFormRef = ref()
const tableRef = ref()
const formRef = ref()

const cardLoading = ref(true)
const treeData = ref([])
const defaultExpandedKeys = ref([])
const treeFieldNames = { children: 'children', title: 'storeName', key: 'id' }
// 左侧树查询
supplierStoresApi
	.supplierStoresList()
	.then((res) => {
		cardLoading.value = false
		if (res !== null) {
			treeData.value =[{
				id:'',
				parentId:'0',
				storeName:'所有门店',
				children:res
			}]
			if (isEmpty(defaultExpandedKeys.value)) {
				//默认展开2级
				treeData.value.forEach((item) => {
					// 因为0的顶级
					if (item.parentId === '0') {
						defaultExpandedKeys.value.push(item.id)
						// 取到下级ID
						if (item.children) {
							item.children.forEach((items) => {
								defaultExpandedKeys.value.push(items.id)
							})
						}
					}
				})
			}
		}
	})
	.finally(() => {
		cardLoading.value = false
	})
// 点击树查询
const treeSelect = (selectedKeys) => {
	if (selectedKeys.length > 0) {
		searchFormState.value.parentId = selectedKeys.toString()
	} else {
		delete searchFormState.value.parentId
	}
	tableRef.value.refresh(true)
}
const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
const columns = [
	{
		title: '仓库名称',
		dataIndex: 'storeName'
	},
	{
		title: '所属门店',
		dataIndex: 'parentIdName'
	},
	{
		title: '仓库图片',
		dataIndex: 'storeImage'
	},
	{
		title: '仓库电话',
		dataIndex: 'storeTel'
	},
	{
		title: '仓库地址',
		dataIndex: 'storeAddress'
	},
	{
		title: '负责人',
		dataIndex: 'storePerson'
	},
	{
		title: '排序码',
		dataIndex: 'sortCode'
	}
]
// 操作栏通过权限判断是否显示
if (hasPerm(['supplierWarehouseStoragesEdit', 'supplierWarehouseStoragesDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
}
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
	// columns数字类型字段加入 needTotal: true 可以勾选自动算账
	alert: {
		show: true,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const loadData = (parameter) => {
	const searchFormParam = cloneDeep(searchFormState.value)
	return supplierStoragesApi.supplierStoragesPage(Object.assign(parameter, searchFormParam)).then((data) => {
		return data
	})
}
// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 删除
const deleteSupplierStorages = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	supplierStoragesApi.supplierStoragesDelete(params).then(() => {
		tableRef.value.refresh(true)
	})
}
// 批量删除
const deleteBatchSupplierStorages = (params) => {
	supplierStoragesApi.supplierStoragesDelete(params).then(() => {
		tableRef.value.clearRefreshSelected()
	})
}
const xnparentIdPageSelectRef = ref();
const parentIdSelector = (param) => {
	return supplierStoresApi.supplierStoresPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
nextTick(() => {
	xnparentIdPageSelectRef.value.onPage({current: 1});
});
</script>
